﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WpfControlDemo.Utils;

namespace WpfControlDemo.ViewModels.MVVM
{
    public class CommandViewModel : ViewModelBase // 继承属性改变通知客户端基类
    {
        /// <summary>
        /// 初始化
        /// </summary>
        public CommandViewModel()
        {
            UserName = "UserName";
            UserPwd = "1234";
        }

        private string userName;
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName
        {
            get { return userName; }
            set
            {
                userName = value;
                OnPropertyChanged(); // 执行数据变更通知客户端
            }
        }

        /// <summary>
        /// 用户密码
        /// </summary>
        public string UserPwd { get; set; }

        /// <summary>
        /// 登陆按钮
        /// </summary>
        public RelayCommand LoginCmd
        {
            get
            {
                return new RelayCommand(o =>
                {  // 执行Command绑定事件
                    UserName = "Jason";

                    SubPageViewModel vm = new SubPageViewModel(); // 弹出窗口的ViewModel
                    Show("subWin", vm);//显示窗口

                }, o =>
                { // 执行条件检测事件
                    bool bl = UserName == "" ? false : true;
                    return bl; //决定了目标对象的可用性
                });
            }
        }

        /// <summary>
        /// TextBox控件通过事件的方式定义的命令
        /// </summary>
        public RelayCommand NameChangedCmd
        {
            get
            {
                return new RelayCommand(o =>
                {
                    if (UserName.Length > 3)
                    {
                        MessageBox.Show("账号长度超过3");
                    }
                });
            }
        }

        /// <summary>
        /// 使用事件方式绑定的退出程序
        /// </summary>
        public RelayCommand QuitCmd
        {
            get
            {
                return new RelayCommand(o =>
                {
                    MessageBox.Show("退出程序");
                    Application.Current.Shutdown();
                });
            }
        }
       
        /// <summary>
        /// 绑定事件按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("事件绑定成功");
        }
    }
}
